from google.appengine.api import users
from google.appengine.ext import db


import datetime

def str2date(string):
    splitted = string.split("-")
    return datetime.date(year=int(splitted[0]), month=int(splitted[1]), day=int(splitted[2]))



class UserModel(db.Model):
    nick                = db.StringProperty(required=True) #unique!
    fullname            = db.StringProperty(required=True)
    description         = db.TextProperty(required=False)
    password            = db.StringProperty(required=True)
    
    dict_list = ["nick", "fullname", "description", "password"]
    
    
    @staticmethod
    def nickIsUnique(nick):
        query = UserModel.all().filter('nick', nick)
        entity = query.get()
        if entity:
            return False
        return True
    
    @staticmethod
    def getUser(nick):
        query = UserModel.all().filter('nick', nick)
        entity = query.get()
        if entity:
            return entity
        return None
    
    def to_dict(self):
        return dict([(p, unicode(getattr(self, p))) for p in self.dict_list])
    
    
    
    
class GroupModel(db.Model):
    name        = db.StringProperty(required=True)
    description = db.StringProperty(required=True) 
    deadline    = db.DateProperty(required=False)
    github      = db.StringProperty(required=False)
    
    dict_list = ["name", "description", "deadline", "github"]
    
    @staticmethod
    def nameIsUnique(name):
        query = GroupModel.all().filter('name', name)
        entity = query.get()
        if entity:
            return False
        return True
    
    def userIsMember(self, user):
        check = UserGroupModel.all().filter("group", self).filter("user", user).get()
        if not check:
            return False
        if(check.role != "manager" and check.role != "developer"):
            return False
        return True
    
    def to_dict(self):
        d = dict([(p, unicode(getattr(self, p))) for p in self.dict_list])
        d["key"] = str(self.key())
        return d
    
    def to_dict_with_user_role(self):
        d = dict([(p, unicode(getattr(self, p))) for p in self.dict_list])
        d["key"] = str(self.key())
        
        relation = UserGroupModel.all().filter("group", self)
        list = []
        for r in relation:
            list.append(r.user.to_dict())
        d["users"] = list
        return d
    
    
class UserGroupModel(db.Model):
    user = db.ReferenceProperty(UserModel, required=True, collection_name='users')
    group = db.ReferenceProperty(GroupModel, required=True, collection_name='groups') 
    role = db.StringProperty(choices=('manager', 'developer', 'none'))
    
    @staticmethod
    def getGroupsByUser(user):
        return UserGroupModel.gql("WHERE user = :1", user)
    
    
    

class TaskModel(db.Model):
    name                  = db.StringProperty(required=True)
    description           = db.StringProperty(required=False)
    deadline              = db.DateProperty(required=True)
    checked               = db.BooleanProperty(default=True)
    group                 = db.ReferenceProperty(GroupModel, required=True)
    user                  = db.ReferenceProperty(UserModel, required=True, collection_name='user')
    parent_task           = db.SelfReferenceProperty(required=False)
    
    
    dict_list = ["name", "description", "deadline", "checked"]
    
    
    def to_dict(self):
        d = dict([(p, unicode(getattr(self, p))) for p in self.dict_list])
        d["key"] = str(self.key())
        d["user"] = self.user.to_dict()
        if self.parent_task:
            d["parent"] = self.parent_task.to_dict()
        else:
            d["parent"] = "None"
        return d
    
class MessageModel(db.Model):
    sender      = db.ReferenceProperty(UserModel, required=True, collection_name="sender")
    recipient   = db.ReferenceProperty(UserModel, required=True, collection_name="recipient")
    group       = db.ReferenceProperty(GroupModel, required=True)
    date        = db.DateTimeProperty(required=True, auto_now=True)
    title       = db.StringProperty(required=True)
    text        = db.TextProperty(required=True)
    dict_list = ["date", "title", "text",]
    
    def to_dict(self):
        d = dict([(p, unicode(getattr(self, p))) for p in self.dict_list])
        d["key"] = str(self.key())
        d["sender"] = self.sender.to_dict()
        d["recipient"] = self.recipient.to_dict()
        d["group_key"] = str(self.group.key())
        return d
    
